﻿namespace 主键
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            using(var db = new MyContext())
            {
                Dog dog = new Dog();

                ////可以手动给Id赋值。也可以不赋值，EFCore会自动赋值,但需要把对象加入Dogs之后才会赋值。
                //dog.Id = Guid.NewGuid();
                //dog.Name = "Dog1";
                //Console.WriteLine(dog.Id);
                //db.Dogs.Add(dog);
                //Console.WriteLine(dog.Id);
                //await db.SaveChangesAsync();
                //Console.WriteLine(dog.Id);

                ////让EF Core自己给Guid类型赋值                
                //dog.Name = "Dog2";
                //Console.WriteLine(dog.Id);  //这里全是0
                //db.Dogs.Add(dog);
                //Console.WriteLine(dog.Id);  //这里就有值了
                //await db.SaveChangesAsync();
                //Console.WriteLine(dog.Id);  //这里和上面的值相同


                //因为是数据库生成的值，所以SaveChanges后会自动把主键的值更新到Id属性
                //测试自增长主键
                Cat cat = new Cat();
                cat.Name = "Mimi";
                Console.WriteLine(cat.Id);      //0
                db.Cats.Add(cat);
                Console.WriteLine(cat.Id);      //0
                await db.SaveChangesAsync();
                Console.WriteLine(cat.Id);      //有值
            }
            
        }
    }
}
